/**
 * 加载区域tree
 */

function loadUserconfigTree(){
	
	
	$.ajax({
		   type: "POST",
		   url: "../userconfig/user_config!getDeptTree.xhtml?node=",
		   success: function(msg){
			   zNodes = eval(msg);
			 	treeObj = $.fn.zTree.init($("#areaTree"), setting, zNodes);
			//	treeObj.expandNode(treeObj.getNodes()[0], true, true, true);
				//treeObj.setting.async.url = "../userconfig/user_config!list.xhtml?deptid="+treeObj.getNodes()[0].id;
				//treeObj.reAsyncChildNodes(treeObj.getNodes()[0], "refresh");
				
				selectNodes();//默认选中第一个节点
			 	
			 	
		   }
		});
}



function selectNodes(){
	var treeObj = $.fn.zTree.getZTreeObj("areaTree");
	var nodes = treeObj.getNodes();
	
	if (nodes.length>0) {
		treeObj.selectNode(nodes[0]);
	
		var deptids=nodes[0].id;
		
		/*
		var nodes = treeObj.getSelectedNodes();
		var treeNode = nodes[0];
		var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
		for(var i=0;i<treeNode.children.length;i++) {
		    var childNode = treeNode.children[i];
		    alert('childNode.name: '+childNode.id);
		}*/
		
		
		var nodes12 = treeObj.getSelectedNodes();
		  var str ='' ;
		  str = getAllChildrenNodes(nodes12[0],str);
		     if(str!=""){
		    	 deptids=deptids+str;
		    	//alert(deptids); //所有叶子节点ID
		    	 
		     }
		     
		     
		     $.ajax({
				  url: "../userconfig/user_config!deptidsPut.xhtml",
				  type: "POST",
				  data:{deptids:deptids,deptid:nodes[0].id},
				  success: function(date){
					  $("#iframepage").attr("src","../userconfig/user_config!list.xhtml?deptid="+nodes[0].id);    
				//alert(date);
			    }
				});
		     
		     
		//  $("#iframepage").attr("src","../userconfig/user_config!list.xhtml?deptids="+deptids+"&deptid="+nodes[0].id);
		  
		  $("#deptid").val(nodes[0].id);
		   $("#test").html(nodes[0].name);  
		//默认首次加载
	//	setMcm(nodes[0].id);
	}
}


//设置栏目值
function setMcm(id){
$.ajax({
		  url: "../column/mColumnManageList!findByMcId.action?id="+id,
		  cache: false,
		  success: function(html){
			    
		   var mcm = eval("["+html+"]");
		   if(mcm.length>0){
		      //alert(mcm[0].pid);
		      $("#pid").val(mcm[0].pid);
		      $("#mcmId").val(mcm[0].id);
		      $("#xpid").val(mcm[0].pid);
		      $("#xmcmId").val(mcm[0].id);
		      $("#mcmName").val(mcm[0].mcmName);
		      var state=mcm[0].state;
		      //alert(state);
		      $("#mcmstate option[value="+state+"]").attr("selected", true);
		      var channelType=mcm[0].channelType;
		     // alert(channelType);
		      $("#mcmchannelType option[value="+channelType+"]").attr("selected", true);
		      $("#mcmNo").val(mcm[0].mcmNo);
		      $("#mcmUrl").val(mcm[0].mcmUrl);
		      $("#columnDescribe").val(mcm[0].columnDescribe);
		   }
			   
	    }
		});

}


//设置栏目项值
function setMcmXing(id){
$.ajax({
		  url: "../column/mColumnManageList!findByMcIdXing.action?id="+id,
		  cache: false,
		  success: function(html){
		   if(html){
		   var data = eval("("+html+")");
		   var mcm=data.m;
		   var mglist=data.list;
		   if(mcm){
		      //alert("==="+mcm.id);
		      //alert(mcm.pid);
		      $("#pid").val(mcm.id);
		      $("#mcmId").val(mcm.id);
		      $("#xpid").val(mcm.pid);
		      $("#xmcmId").val(mcm.id);
		      $("#xmcmName").val(mcm.mcmName);
		      var state=mcm.state;
		      //alert(state);
		      $("#xmcmstate option[value="+state+"]").attr("selected", true);
		      var channelType=mcm.channelType;
		     // alert(channelType);
		      $("#xmcmchannelType option[value="+channelType+"]").attr("selected", true);
		      $("#xsystemPrincipal").val(mcm.systemPrincipal);
		      $("#xmobileno").val(mcm.mobileno);
		      $("#xmcmCode").val(mcm.mcmCode);
		      $("#xmcmNo").val(mcm.mcmNo);
		      $("#xmcmUrl").val(mcm.mcmUrl);
		      $("#xcolumnDescribe").val(mcm.columnDescribe);
		      $("#logoUrl").val(mcm.logoImg);
		      $("#logoImg").attr("src",mcm.logoImg);
		       var isLogin=mcm.isLogin;
		       $("#xisLogin option[value="+isLogin+"]").attr("selected", true);//是否需要登陆
		       $("#xsystemBelong").val(mcm.systemBelong);//系统名称
		       $("#xsystemId").val(mcm.systemId);//系统id
		   }
		   //设置角色分配select值
		   if(mglist.length>0){
		      $("#productvalue").empty(); 
		      $("#groupids").val("");
		      $("#mcgTypeValue").val("");
		       $("#mcgIds").val("");
		      var grids="";
		      var types="";
		      var mcgIds="";
		      for(i=0;i<mglist.length;i++){
		         var mgIdx=mglist[i].id;
		        var mgId=i+"_"+mglist[i].mgId;
		        var mgName=mglist[i].mgName;
		        var type=mglist[i].type;
		        $("#productvalue").append("<option value='"+mgId+"'>"+mgName+"</option>"); 
		             var id=(grids == "") ? mgId : "," + mgId;
					 grids+=id;
					 var tt=(types == "") ? type : "," + type;
					 types+=tt;
					 
					 var idx=(mcgIds == "") ? mgIdx : "," + mgIdx;
					 mcgIds+=idx;
		      }
		      $("#mcgIds").val(mcgIds);
		      $("#groupids").val(grids);
		      $("#mcgTypeValue").val(types);
		   }else{
		   $("#productvalue").empty();
		   $("#groupids").val("");
		   $("#mcgTypeValue").val("");
		   $("#mcgIds").val("");
		   
		   }
		 }else{
		 
		  $("#productvalue").empty();
		   $("#groupids").val("");
		   $("#mcgTypeValue").val("");
		    $("#mcgIds").val("");
		 }
	    }
		});

}


var setting = {
		async: {
			enable: true//,
		//	url: getUrl
		//	autoParam: ["id", "name"]
		},
		check: {
			enable: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		view: {

			dblClickExpand: true,

			showLine: true,

			selectedMulti: false,

			expandSpeed: ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast"

		},

		callback: {
			beforeExpand: beforeExpand,
			onAsyncSuccess: onAsyncSuccess,
			onAsyncError: onAsyncError,
			onClick: zTreeOnClick

		}
	};

var alss="";
function checkAllParents(treeNode){
	if (treeNode==null || treeNode.pId==0) {
	return ;
	}
	else
	{
	treeNode.checked=true;
	if(treeNode.getParentNode()!=null){
	alss=treeNode.getParentNode().name+"/"+alss;
	}
	checkAllParents(treeNode.getParentNode());
	}
	}

function getUrl(treeId, treeNode) {
	//alert(treeNode.id);
	return "../userconfig/user_config!loadTree.xhtml?node="+treeNode.id;
}
function zTreeOnClick(e, treeId, treeNode) {
    
	var deptids=treeNode.id;
	  var str ='' ;
	  str = getAllChildrenNodes(treeNode,str);
	     if(str!=""){
	    	 deptids=deptids+str;
	    	// alert(deptids); //所有叶子节点ID
	    	 
	     }
	
	     $.ajax({
			  url: "../userconfig/user_config!deptidsPut.xhtml",
			  type: "POST",
			  data:{deptids:deptids,deptid:treeNode.id},
			  success: function(date){
				  $("#iframepage").attr("src","../userconfig/user_config!list.xhtml?deptid="+treeNode.id);    
			//alert(date);
		    }
			});
	     
	  // $("#iframepage").attr("src","../userconfig/user_config!list.xhtml?deptids="+deptids+"&deptid="+treeNode.id); 
	   alss="";
	   $("#deptid").val(treeNode.id);
	   checkAllParents(treeNode);
	   $("#test").html(alss+treeNode.name);  
	   
}

	
function beforeExpand(treeId, treeNode) {
	 var treeObj = $.fn.zTree.getZTreeObj("areaTree");
	 treeObj.setting.async.url = getUrl(treeId,treeNode);
}

function onAsyncSuccess(event, treeId, treeNode, msg) {
	 var treeObj = $.fn.zTree.getZTreeObj("areaTree");
	 treeObj.setting.async.url = getUrl(treeId,treeNode);
}

function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
	var zTree = $.fn.zTree.getZTreeObj("areaTree");
	alert("异步获取数据出现异常。");
	treeNode.icon = "";
	zTree.updateNode(treeNode);
}


$(document).ready(function(){

	loadUserconfigTree();//加载tree
	
	
});



function getAllChildrenNodes(treeNode,result){
    if (treeNode.isParent) {
      var childrenNodes = treeNode.children;
      if (childrenNodes) {
          for (var i = 0; i < childrenNodes.length; i++) {
              result += ',' + childrenNodes[i].id;
              result = getAllChildrenNodes(childrenNodes[i], result);
          }
      }
  }
  return result;
}